/* 
const baseUrl = 'http://api-breakingnews-web.itheima.net'
// 1.渲染数据————————————————————————————————————————————————
function getUserInfo(){
    axios.get(`${baseUrl}/my/userinfo`,{
        //传递token 请求 不然会失败
        headers:{
            Authorization:localStorage.getItem('token')
        }
    }).then(res =>{
        console.log(res);
        // 结构 判断是否获取信息成功
        const { status } =res.data
        if(status === 0){
            //成功之后获取用户信息  解构
            let {email,id,nickname,username} = res.data.data
            // username = nickname || username
            $('input[name=username]').val(username)
            $('input[name=nickname]').val(nickname)
            $('input[name=email]').val(email)
            $('input[name=id]').val(id)
        }
        // 判断认证失败 就清除token 和跳转到登录页面
        if(status === 1){
            localStorage.removeItem('token')
            location.href='/login.html'
        }
    })
}
getUserInfo()
// 2.提交————————————————————————————————————————————————
    $('form').on('submit',e => {
        e.preventDefault()
        const data = $('form').serialize()
        console.log(data);
        axios.post(`${baseUrl}/my/userinfo`,data,{
            headers:{
                Authorization: localStorage.getItem('token')
            }
        }).then(res => {
            console.log(res);
            if (res.data.status === 0){
                window.parent._getUser()
            }
        })
    })
// 3.重置按钮————————————————————————————————————————————————
$('.my-reset').on('click',e =>{
    e.preventDefault()
    getUserInfo()
})

let form = layui.form;
form.verify({ // 指定规则名和对应的验证规则
    nickname: [ // 昵称
        /^[\u4E00-\u9FA5]+$/,
        '昵称只能是中文'
    ],
}) */

const baseUrl = 'http://api-breakingnews-web.itheima.net'

//发送请求渲染数据
function getUserInfo(){
    axios.get(`${baseUrl}/my/userinfo`, {
        headers: {
            Authorization: localStorage.getItem('token')
        }
    }).then(res => {
        // console.log(res);
    
        const { status } = res.data
        if (status === 0) {
        const { username, nickname, email ,id} = res.data.data
            $('input[name=username]').val(username)
            $('input[name=nickname]').val(nickname)
            $('input[name=email]').val(email)
            $('input[name=id]').val(id)
        }
        if (status === 1) {
            localStorage.removeItem('token')
            location.href = '/login.html'
        }
    })
}
getUserInfo()
// 修改
$('.layui-form').on('submit', e => {
    e.preventDefault()
    let data = $('.layui-form').serialize()
    axios.post(`${baseUrl}/my/userinfo`, data, {
        headers: {
            Authorization: localStorage.getItem('token')
        }
    }).then(res => {
        if (res.data.status === 0) {
            window.parent.getUser()
        }
    })
})
//重置
$('.my-reset').on('click',e=>{
    getUserInfo()
})


// 验证
let form = layui.form;
form.verify({ // 指定规则名和对应的验证规则
    nickname: [ // 昵称
        /^[\u4E00-\u9FA5]+$/,
        '昵称只能是中文'
    ],
})